//获取应用实例
const app = getApp();
import { PNT, setNavBarTitle, pushPagePath, checkMobile } from "../../../utils/utils";
import { SendSms, GetSendVcode, UpdateUserInfoByCode } from "../../../utils/apis";

Page({
  data: {
    hidden: true,
    btnValue: '获取短信验证码',
    btnDisabled: false,
   
    phone: '',
    code: '',
    second: 60,
    flag: true,
    sms: '获取短信验证码',
    code: true,
  },
  onLoad: function () {
    this.loginModal = this.selectComponent("#login-modal");
    if (!this.loginModal.check()) {
      wx.navigateBack({
        delta: 1,
      })
    }
    var info = wx.getStorageSync('userInfo');
    let type = 'uppwd' + info.user_id;
    this.setData({
      info: info,
      phone: info.mobile_phone,
      type: type,
      genre: 'pwd',
    })

  },
  
  //获取验证码

  sms: function (e) {
    var that = this;
    if (!this.data.code) {
      var sms = that.data.sms;
      wx.showToast({
        title: sms,
        icon: 'none'
      })
      return;
    }
    that.setData({
      code: false
    })
    var type = this.data.type;
    var phone = this.data.info.mobile_phone;
    // phone = '17310606312';
    var user_id = this.data.info.user_id;
    var data = { type, phone, user_id };
    GetSendVcode(data).then(res => {
      if (res.error_code == 10000) {
        wx.showToast({
          title: '验证码发送成功',
        })
        that.time()
      }else{
        this.setData({
          code:true,
        })
      }
    }).catch(function (reason) {
      wx.showToast({
        title: '服务器忙',
      })
      that.onLoad();
    })
  },
  time: function (e) {
    var that = this;
    var time = 120;
    var inter = setInterval(function () {
      time--;
      that.setData({
        sms: "请" + time + "秒后再发"
      })
      if (time == 0) {
        that.setData({
          sms: '获取短信验证码',
          code: true,
        })
        clearInterval(inter)
      }
    }, 1000)
  },

  //手机号输入
  bindCodeInput(e) {
    var items = e.currentTarget.dataset;
    var val = e.detail.value;
    if (items.type == 'vcode') {
      this.setData({
        vcode: val
      })
    }
    if (items.type == 'pwd') {
      this.setData({
        passwd: val
      })
    }
  },

  //保存
  save(e) {
    if (!this.data.flag) {
      wx.showToast({
        title: '数据提交中',
        icon: "none"
      })
      return;
    }
    var data = {};
    data.user_id = this.data.info.user_id;
    data.type = this.data.type;
    data.genre = this.data.genre;
    if (!this.data.vcode) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return;
    }
    if (!this.data.passwd) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return;
    }
    this.data.flag = false;
    data.phone = this.data.info.mobile_phone;
    data.vcode = this.data.vcode;
    data.passwd = this.data.passwd;
    UpdateUserInfoByCode(data).then(res => {
      this.data.flag = true;
      if (res.error_code == 10000) {
        wx.setStorageSync('userInfo', res.data)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        return;
      }
    })

    //省略提交过程
  }
})